﻿Imports FL_8x0.Common
Imports FL_8x0.Model
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf

Namespace FL_8x0.Viewmodel

    Public Class LoginViewModel : Inherits ViewModelBase

        Private _loginCommand As RelayCommand(Of Window)   '点击“对号”触发命令
        Private _clearCommand As RelayCommand              '点击“左箭头”触发命令
        Private _closeCommand As RelayCommand(Of Window)   '点击“关机”触发命令

        Private _userModel As UserModel



        Public Sub New()
            UserModel = New UserModel()
            LoginCommand = New RelayCommand(Of Window)(Sub(window) Login(window))
            CloseCommand = New RelayCommand(Of Window)(Sub(window) Close(window))
            ClearCommand = New RelayCommand(Sub()
                                                UserModel.Username = ""
                                                UserModel.Password = ""
                                            End Sub)
            UserModel.Username = g_udtOtherSet.strUserName
            UserModel.Password = g_udtOtherSet.strPassword

        End Sub

        '=====================================================================
        '函 数 名: Login
        '参    数: 
        '返 回 值:
        '功能描述: 登录
        '=====================================================================
        Private Sub Login(win As Window)
            If CheckInput() Then
                Dim result = CheckUser(UserModel.Username, UserModel.Password)
                If result Then
                    log.UserName = SystemData.g_strUserName
                    log.WriteLogInfo("登录", "用户名:" & SystemData.g_strUserName) 'log痕迹记录
                    win.DialogResult = True
                End If
            End If
        End Sub

        '=====================================================================
        '函 数 名: Close
        '参    数: 
        '返 回 值:
        '功能描述: 关闭
        '=====================================================================
        Private Sub Close(window As Window)
            window.Close()
        End Sub


        '=====================================================================
        '函 数 名: CheckInput
        '参    数: 
        '返 回 值:
        '功能描述: 数据有效性检测
        '=====================================================================
        Private Function CheckInput() As Boolean

            If UserModel.Username = "" Then
                log.WriteLogError("输入参数检查", "[用户名]不能为空！")
                Return False
            End If

            If UserModel.Username.Length > 250 Then
                log.WriteLogError("输入参数检查", "[用户名]长度不能超过250个字符！")
                Return False
            End If

            If UserModel.Password = "" Then
                log.WriteLogError("输入参数检查", "[密码]不能为空！")
                Return False
            End If

            If UserModel.Password.Length > 250 Then
                log.WriteLogError("输入参数检查", "[密码]长度不能超过250个字符！")
                Return False
            End If

            Return True
        End Function



        Public Property LoginCommand As RelayCommand(Of Window)
            Get
                Return _loginCommand
            End Get
            Set
                _loginCommand = Value
            End Set
        End Property

        Public Property ClearCommand As RelayCommand
            Get
                Return _clearCommand
            End Get
            Set
                _clearCommand = Value
            End Set
        End Property

        Public Property CloseCommand As RelayCommand(Of Window)
            Get
                Return _closeCommand
            End Get
            Set
                _closeCommand = Value
            End Set
        End Property


        Public Property UserModel As UserModel
            Get
                Return _userModel
            End Get
            Set
                _userModel = Value
            End Set
        End Property
    End Class
End Namespace